package SocialMediaSentiment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.HashMap;


public class Wordcounter{
    public static void main(String[] args) {
        File file = new File("mediumnCorrectedTrainSetNonZero.txt");
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
        
        Map<String, Integer> woordenlijst = new HashMap<String, Integer>();
        int value = 0;
        
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        char[] chararray = new char[contents.length()];
        contents.getChars(0, contents.length(), chararray, 0);
        String key;
       
        StringTokenizer st = new StringTokenizer(contents.toString(), " \r\n\t\f,.!$%^&*()[]{}-=\\;<>/?");
        while (st.hasMoreTokens()) 
        {
        	key = st.nextToken();
          	if(woordenlijst.containsKey(key)){value =  woordenlijst.get(key) + 1;}else{value=1;}
          	woordenlijst.put(key, value);
        }
      
        try{
        	  // Create file 
        	  FileWriter fstream = new FileWriter("out.txt");
        	  BufferedWriter out = new BufferedWriter(fstream);
              for( String keys : woordenlijst.keySet() )
              {
            	  out.write(keys+" "+woordenlijst.get(keys));
            	  out.newLine();
              }
              //Close the output stream
        	  out.close();
        	  }catch (Exception e){//Catch exception if any
        	  System.err.println("Error: " + e.getMessage());
        	  }
    }
    
    
}
